package com.jintoufs.service.baseinfo.impl;

import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.baseinfo.CoinDAO;
import com.jintoufs.dao.baseinfo.SiteCoinDAO;
import com.jintoufs.dao.baseinfo.SiteDAO;
import com.jintoufs.domain.baseinfo.Coin;
import com.jintoufs.domain.baseinfo.Site;
import com.jintoufs.domain.baseinfo.SiteCoin;
import com.jintoufs.service.baseinfo.SiteService;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoemo
 */
@Repository
public class SiteServiceImpl implements SiteService {
    @Resource
    private SiteDAO siteDAO;
    @Resource
    private SiteCoinDAO siteCoinDAO;
    @Resource
    private CoinDAO coinDAO;

    @Override
    public List<Map<String, Object>> queryByParams(Map<String, Object> params) {
        return siteDAO.queryByParams(params);
    }

    @Override
    @Transactional
    public void insert(Site entity) {
        siteDAO.insert(entity);
        buildSiteCoinFromSelect(entity);
        buildSiteCoinsFromText(entity);
    }

    private void buildSiteCoinsFromText(Site entity) {
        //如果币种字符串不为空，则需要增加币种
        if (org.apache.commons.lang.StringUtils.isNotEmpty(entity.getCoinsText())) {
            String coinsText = entity.getCoinsText();
            String[] coinArr = coinsText.split(",");
            for (String coinCode : coinArr) {
                Coin coin = coinDAO.selectByCoinCode(coinCode);
                if (coin != null) {
                    //查询是否该网站已经存在该币种
                    Map<String, Object> map = new HashMap();
                    map.put("siteId", entity.getId());
                    map.put("coinId", coin.getId());
                    List<Map<String, Object>> list = siteCoinDAO.queryByParams(map);
                    if (list.isEmpty()) {
                        SiteCoin siteCoin = new SiteCoin();
                        siteCoin.setSiteId(entity.getId());
                        siteCoin.setCoinRename(coinCode);
                        siteCoin.setCoinId(coin.getId());
                        siteCoinDAO.insert(siteCoin);
                    }
                }
            }
        }
    }

    private void buildSiteCoinFromSelect(Site entity) {
        //siteCoinDAO.deleteByParams(params);
        if (!StringUtils.isEmpty(entity.getCoinIds())) {
            String array[] = entity.getCoinIds().split(",");
            for (String idStr : array) {
                Integer id = Integer.parseInt(idStr);
                Map<String, Object> params = new HashMap();
                params.put("coinId", id);
                params.put("siteId", entity.getId());
                List<Map<String,Object>>list=siteCoinDAO.queryByParams(params);
                if(list.isEmpty()){
                    SiteCoin siteCoin = new SiteCoin();
                    Coin coin = coinDAO.selectByPrimaryKey(id);
                    siteCoin.setCoinId(id);
                    siteCoin.setSiteId(entity.getId());
                    siteCoin.setCoinRename(coin.getCode());
                    siteCoinDAO.insert(siteCoin);
                }
            }
        }
    }

    @Override
    @Transactional
    public void updateByPrimaryKey(Site entity) {
        siteDAO.updateByPrimaryKey(entity);
    }


    @Override
    @Transactional
    public void updateSite(Site entity) {
        siteDAO.updateByPrimaryKey(entity);
        buildSiteCoinFromSelect(entity);
        buildSiteCoinsFromText(entity);
    }

    @Override
    @Transactional
    public void deleteByPrimaryKey(Integer id) {
        Map<String, Object> params = new HashMap();
        params.put("siteId", id);
        siteCoinDAO.deleteByParams(params);
        siteDAO.deleteByPrimaryKey(id);
    }

    @Override
    public List<Map<String, Object>> getByFilter(QueryFilters filters) {
        return siteDAO.getByFilter(filters);
    }

    @Override
    public Site selectByPrimaryKey(Integer id) {
        return siteDAO.selectByPrimaryKey(id);
    }
}
